
CUR_DIR =$(shell pwd)

NP      =8
NAME    =map
RNAME   =mapreader
MODNAME =model_new
QUEUE   =mpi_2
MPIRUN  =bsub -a intelmpi -q $(QUEUE) -n $(NP) mpirun.lsf
FC      =mpiifort
CC      =mpiicc
CPP     =mpicpc
MAPPATH =../../MAP_LIB
OPT     =-O2 -warn -traceback -vec-report0 -module $(MAPPATH)
#for checking
OPT2    =-O2 -warn -traceback -vec-report0 -check bounds -check pointers -check uninit -module $(MAPPATH)
OPTC    =-Wall -pipe -O2
SILOPATH=/gpfssan1/home/fangc/Silo/include
SILOLIB =/gpfssan1/home/fangc/Silo/lib/libsilo.a
CHECK   =
ifdef CHECK
   OPT=$(OPT2)
endif

include $(MAPPATH)/Makefile.p

FILES   = \
$(FILES_MAP_P) \
model_variables.f90 \
model_resis.f90 \
bnd_user.f90 \
damping_zone.f90

FILES_M = $(MAPPATH)/main.f90
FILES_D = $(MAPPATH)/data_manage.f90 \
          $(MAPPATH)/data_collect.f90 \
          $(MAPPATH)/extremum_calculate.f90 \
          $(MAPPATH)/write_silo.f90 \
          $(MAPPATH)/write_vtk_rectilinear.f90 \
          $(MAPPATH)/write_vtk_unstructured.f90 \
          $(MAPPATH)/write_vtu_data.f90 \
          $(MAPPATH)/write_vtu_header.f90 \
          $(MAPPATH)/write_mapvis.f90
FILES_DC= $(MAPPATH)/data_manage.c

OBJS    =$(FILES:.f90=.of)
OBJS_D  =$(FILES_D:.f90=.of)
OBJS_M  =$(FILES_M:.f90=.of)
OBJS_DC =$(FILES_DC:.c=.oc)

$(NAME):$(OBJS) $(OBJS_M)
	$(FC) $(OPT) -o $(NAME) $(OBJS) $(OBJS_M)
	@echo
	@echo Compile completed!

%.of:%.f90
	$(FC) $(OPT) -c -o $@ $< -I$(SILOPATH)

run:$(NAME)
	mpiexec -n $(NP) $(PWD)/$(NAME)

submit:$(NAME)
	$(MPIRUN) $(PWD)/$(NAME)

rddt:$(OBJS_D)
	$(FC) $(OPT) -o $(RNAME) $(OBJS_D) $(SILOLIB)
#	$(FC) $(OPT) -o rddt.exe $(MAPPATH)/data_manage.f90
	mpiexec -n $(NP) $(PWD)/$(RNAME)

%.oc:%.c
	$(CC) $(OPTC) -I$(SILOPATH) -I$(CUR_DIR) -c -o $@ $<

rddtc:$(OBJS_DC)
	$(CC) $(OPTC) -o $(RNAME) $(OBJS_DC) $(SILOLIB)
	rm $(OBJS_DC)
	mpiexec -n $(NP) $(PWD)/$(RNAME)

newmodel:
	mkdir -p ../$(MODNAME)
	cp *.f90 parameters Makefile *.pro ../$(MODNAME)

kill:
	@ bkill -r `ls output.* | awk -F '.' 'END {print $$2}'`

tail:
	@ tail -f `ls output.* | awk "END{print}"`

clean:
	rm -f $(NAME) $(RANME) $(OBJS_D) *genmod* *~ *\* input *.mod *.of *.oc *.include
	rm -rf `ls -d */ 2> /dev/null`
	@echo
	@echo Clean completed!

cleanrddt:
	rm $(OBJS_D)
	@echo
	@echo Cleanrddt completed!
	
cleanall:
	rm -f $(OBJS) $(OBJS_M) $(OBJS_D) $(NAME) $(RNAME) *genmod* *~ *\* input *.mod output.* *.include
	cd $(MAPPATH); rm -f *genmod* *~ *\* input *.mod
	rm -rf `ls -d */ 2> /dev/null`
	@echo
	@echo Cleanall completed
